package com.ebookstore.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebookstore.common.PagedListHolder;
import com.ebookstore.dto.Bookview;
import com.ebookstore.exception.EBookStoreException;

public class ViewBookAction extends BaseAction {
	/**
	 * 根据图书分类查询图书
	 */
	public ActionForward byCategory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//获得分类的id
		String categoryId = request.getParameter("cid");
		int id = -1;
		try {
			id = Integer.parseInt(categoryId);
		} catch (Exception e) {
			;
		}
		
		if (id > -1) {
			//查询该分类的图书
			List<Bookview> data = getEBookStore().getBookService()
					.findBookByCategory(categoryId);
			//将查询结果放入到分页器中
			PagedListHolder bookList = new PagedListHolder(data);
			//设置每页所显示的数据条数
			bookList.setPageSize(2);
			//放Session中
			request.getSession().setAttribute("ViewBooksByCategory_bookList",
					bookList);
			request.setAttribute("bookList", bookList);
		}
		return mapping.findForward("success");
	}
	/**
	 * 下一页
	 */
	public ActionForward nextPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//取出Session中的分页器
		PagedListHolder bookList = (PagedListHolder) request.getSession()
				.getAttribute("ViewBooksByCategory_bookList");
		//取出下一页数据
		bookList.nextPage();
		request.setAttribute("bookList", bookList);
		return mapping.findForward("success");
	}
	/**
	 * 上一页
	 */
	public ActionForward previousPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//取出Session中的分页器
		PagedListHolder bookList = (PagedListHolder) request.getSession()
				.getAttribute("ViewBooksByCategory_bookList");
		//取出上一页数据
		bookList.previousPage();
		request.setAttribute("bookList", bookList);
		return mapping.findForward("success");
	}

}
